﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CrmDocman.DataCore;
/// <summary>
/// Summary description for TinTucHelper
/// </summary>
public class TinTucHelper : TWSData
{
	public TinTucHelper()
    {
        m_dao = DataBridge.GetInstance();
    }

    public TinTucDataSet LoadTinTuc()
    {

        TinTucDataSet _tinTucDS = new TinTucDataSet();
        m_dao.FillDataTable(_tinTucDS.TinTuc);
        m_dao.Connection.CloseConnection();
        return _tinTucDS;
    }

    public TinTucDataSet LoadTinTuc(int page, int pageSize, string sortColumn, string sortOrder, int loaisanpham,int lang)
    {

        TinTucDataSet spDs = new TinTucDataSet();
        string sql = "DECLARE @PageNum AS INT; ";
        sql += "DECLARE @PageSize AS INT; ";
        sql += "SET @PageNum = " + page + "; ";
        sql += "SET @PageSize = " + pageSize + "; ";
        sql += "WITH OrdersRN AS ( ";
        sql += "SELECT ROW_NUMBER() OVER(ORDER BY " + sortColumn + " " + sortOrder + ") AS RowNum, * ";
        sql += "FROM dbo.TinTuc where Loai=" + loaisanpham + " and Lang = "+lang+" ) ";
        sql += "select * from OrdersRN ";
        sql += " WHERE RowNum BETWEEN (@PageNum - 1) * @PageSize + 1 AND @PageNum * @PageSize ";
        sql += " order by " + sortColumn + " " + sortOrder;

        m_dao.FillDataTable(spDs.TinTuc, sql);
        m_dao.Connection.CloseConnection();
        return spDs;
    }

    public TinTucDataSet LoadTinTuc(int id)
    {

        TinTucDataSet _tinTucDS = new TinTucDataSet();
        string sql = m_dao.BuildSelectQuery(_tinTucDS.TinTuc);
        sql += " where ID = '" + id + "' order by Id";

        m_dao.FillDataTable(_tinTucDS.TinTuc, sql);
        m_dao.Connection.CloseConnection();
        return _tinTucDS;
    }

    public TinTucDataSet LoadLoaiTinTuc(int loai,int lang)
    {

        TinTucDataSet _tinTucDS = new TinTucDataSet();
        string sql = "select top 1 * from TinTuc where Loai = " + loai + " and Lang = '" + lang + "' order by NgayDang desc";


        m_dao.FillDataTable(_tinTucDS.TinTuc, sql);
        m_dao.Connection.CloseConnection();
        return _tinTucDS;
    }

    public TinTucDataSet LoadTinTuc(int loai, int lang)
    {

        TinTucDataSet _tinTucDS = new TinTucDataSet();
        string sql = m_dao.BuildSelectQuery(_tinTucDS.TinTuc);
        sql += " where Loai = " + loai + " and Lang = '" + lang + "' order by Id desc";

        m_dao.FillDataTable(_tinTucDS.TinTuc, sql);
        m_dao.Connection.CloseConnection();
        return _tinTucDS;
    }

    public bool SaveTinTuc(TinTucDataSet _tinTucDS)
    {
        m_dao.UpdateDataTable(_tinTucDS.TinTuc);
        m_dao.Connection.CloseConnection();
        return true;

    }
}
